module.exports = Behavior({
  lifetimes: {
    created(){
      this._originSetData = this.setData
      this.setData = this._setData
    }
  },
  definitionFilter(defFields){
    const computed = defFields.computed || {}
    const computedKeys = Object.keys(computed)
    const computedCache = {}
    const calcComputed = (scope,insertToData) => {
      let needUpdate = {}
      let data = defFields.data || {}
      for(let key of computedKeys){
        const computedFunc = computed[key]
        const computedValue = computedFunc.call(scope)
        if(computedValue !== computedCache[key]){
          needUpdate[key] = computedCache[key] = computedValue
        }
        if(insertToData){
          data[key] = computedValue
        }
      }
      return needUpdate
    }
    defFields.methods = defFields.methods || {}
    defFields.methods._setData = function(data, callback){
      this._originSetData.call(this, data, callback)
      let needUpdate = calcComputed(this)
      // console.log('needUpdate:',needUpdate)
      if(Object.keys(needUpdate).length){
        this._originSetData.call(this, needUpdate)
      }
    }
    // 初始化computed
    calcComputed(defFields, true) 
    // let needUpdate = calcComputed(defFields)
    // console.log('needUpdate2:',needUpdate)
    // if(Object.keys(needUpdate).length){
    //   defFields.data = Object.assign({}, defFields.data, needUpdate)
    // }
  }
})